// pages/wares/wares.js
const { getWaresHttp } = require("../../../utils/http")
const { titleStr } = require("../../../utils/wares/utils")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    drink: [],
    index: 0,
    option1: [
      { text: '全部商品', value: 0 },
      { text: '活动商品', value: 1 },
    ],
    option2: [
      { text: '默认排序', value: 'a' },
      { text: '好评排序', value: 'b' },
      { text: '价格排序', value: 'c' },
    ],
  },
  onSwitch1Change(e) {
    let option1 = [
      { text: '全部商品', value: 0 },
      { text: '活动商品', value: 1 },
    ]
    this.setData({option1})
  },
  handleSearchClick() {
    wx.navigateTo({
      url: "../waresSearch/waresSearch"
    })
  },
  //商品
  handleRecommend(e) {
    let { aid } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../waresDetail/waresDetail?aid=${aid}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let drinks = await getWaresHttp.getDrinkHttp(0)
    let drink = drinks.data.res
    drink.forEach(item => {
      item.title = titleStr(item.title)
    })
    this.setData({
      drink
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    let index = this.data.index
    index += 20
    let drinks = await getWaresHttp.getDrinkHttp(index)
    let drink = this.data.drink
    drink.push(...drinks.data.res)
    drink.forEach(item => {
      item.title = titleStr(item.title)
    })
    this.setData({
      index,
      drink
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})